home *** CD-ROM | disk | FTP | other *** search
/ Mastering Internet Develo…oft ActiveX Technologies / Mastering Internet Development with ActiveX (1996)(Microsoft).iso / labs / lab06 / formdump / formdump.cpp next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  7.0 KB  |  234 lines

  1. #define WIN32_LEAN_AND_MEAN         // the bare essential Win32 API
  2. #include <windows.h>
  3. #include <ctype.h>                  // for isprint()
  4. #include <httpext.h>
  5.  
  6. #include "keys.h"
  7. #include "html.h"
  8.  
  9. // prototype
  10. void HexDumper (EXTENSION_CONTROL_BLOCK *pECB, LPBYTE lpbyBuf, DWORD dwLength);
  11.  
  12. //
  13. // DllMain allows us to initialize our state variables.
  14. // You might keep state information, as the DLL often remains
  15. // loaded for several client requests.  The server may choose 
  16. // to unload this DLL, and you should save your state to disk,
  17. // and reload it here.  DllMain is called for both loading
  18. // and unloading.  See the Win32 SDK for more info on how
  19. // DLLs load and unload.
  20. //
  21. // An important part of building any DLL is deciding how the
  22. // entry point is going to function.  You may not want an
  23. // entry point at all.  Or, you might call your entry point
  24. // DllMain and add a link option 
  25. //
  26. // -entry:DllMainCRTStartup$(DLLENTRY)
  27. //
  28. // The makefile symbol DLLENTRY is defined in ntwin32.mak, 
  29. // which comes with Win32 SDK and Visual C++.
  30. //
  31. // You might choose to use Visual C++ 2.x or 4.x--go into the
  32. // project settings, link tab, output and specify a the entry
  33. // symbol DllMainCRTStartup$(DLLENTRY).
  34. //
  35. // By the way, $(DLLENTRY) currently resolves to @12, which is
  36. // the ordinal of _CRT_INIT, the C Runtime initializer.
  37. //
  38.  
  39. BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpv)
  40.     {
  41.     // Nothing to do here
  42.     return (TRUE);
  43.     }
  44.  
  45.  
  46. //
  47. //  BOOL WINAPI GetExtensionVersion (HSE_VERSION_INFO *pVersionInfo) 
  48. //
  49. //  Return the version this server is built for.  See <httpext.h> for
  50. //  a prototype.  This function is required by the spec.
  51. //
  52.  
  53. BOOL WINAPI GetExtensionVersion (HSE_VERSION_INFO *pVersionInfo)
  54.     {
  55.     // set version to httpext.h version constants
  56.     pVersionInfo->dwExtensionVersion = MAKELONG (HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
  57.  
  58.     lstrcpyn ((LPTSTR) pVersionInfo->lpszExtensionDesc,
  59.             TEXT("FORMDUMP - A Form Decoder and Dumper"),
  60.             HSE_MAX_EXT_DLL_NAME_LEN);
  61.  
  62.     return TRUE;
  63.     } // GetExtensionVersion()
  64.  
  65.  
  66. //
  67. //  Our entry point:
  68. //
  69. //  BOOL WINAPI HttpExtensionProc (EXTENSION_CONTROL_BLOCK *pECB)
  70. //
  71. //  This function pulls in all inbound data.  A reply page is built
  72. //  so the user can see how forms appear in our key list.
  73. //  This function is also required by the spec.
  74. //
  75.  
  76. DWORD WINAPI HttpExtensionProc (EXTENSION_CONTROL_BLOCK *pECB)
  77.     {
  78.     HKEYLIST hKeyList;
  79.     TCHAR szMsg[128];
  80.     DWORD dwWritten;
  81.  
  82.     // Get the keys sent by the client
  83.     hKeyList = GetKeyList (pECB);
  84.  
  85.     // Send content type
  86.     TCHAR str[] = TEXT("Content-type: text/html\r\n");
  87.     dwWritten = sizeof (str);
  88.     pECB->ServerSupportFunction (pECB->ConnID,
  89.                                  HSE_REQ_SEND_RESPONSE_HEADER,
  90.                                  NULL,
  91.                                  &dwWritten,
  92.                                  (LPDWORD) str);
  93.  
  94.  
  95.     // Create a basic HTML page
  96.     HtmlCreatePage (pECB, TEXT("FormDump.dll Reply"));
  97.     HtmlHeading (pECB, 2, TEXT("Data Sent by Your Form"));
  98.  
  99.     // Report errors
  100.     if (!hKeyList)
  101.         {
  102.         HtmlBold (pECB, TEXT("No keys sent or error decoding keys"));
  103.         }
  104.     else
  105.         {
  106.         HKEYLIST hKey;
  107.  
  108.         // Print a quick overview
  109.         HtmlWriteTextLine (pECB, TEXT("The form you submitted data to just called"));
  110.         HtmlWriteTextLine (pECB, TEXT("the Internet Information Server extension"));
  111.         HtmlWriteTextLine (pECB, TEXT("FormDump.dll.  Here is a listing of what was"));
  112.         HtmlWriteTextLine (pECB, TEXT("received and what variables inside FormDump"));
  113.         HtmlWriteTextLine (pECB, TEXT("have the data."));
  114.         HtmlEndParagraph (pECB);
  115.  
  116.  
  117.         // Loop through all of the keys
  118.         hKey = hKeyList;
  119.         while (hKey)
  120.             {
  121.             // Details about the key
  122.             LPCTSTR lpszKeyName;
  123.             DWORD dwLength;
  124.             BOOL bHasCtrlChars;
  125.             int nInstance;
  126.             HKEYLIST hLastKey;
  127.  
  128.             // We get info, and hKey points to next key in list
  129.             hLastKey = hKey;    //keep this for later
  130.             hKey = GetKeyInfo (hKey, &lpszKeyName, &dwLength,
  131.                                 &bHasCtrlChars, &nInstance);
  132.  
  133.             
  134.             // Build web page
  135.             HtmlBold (pECB, TEXT("Form Key Name (lpszKeyName): "));
  136.             HtmlWriteText (pECB, lpszKeyName);
  137.             HtmlLineBreak (pECB);
  138.  
  139.             HtmlBold (pECB, TEXT("Length of Key Data (dwLength): "));
  140.             wsprintf (szMsg, TEXT("%u"), dwLength);
  141.             HtmlWriteText (pECB, szMsg);
  142.             HtmlLineBreak (pECB);
  143.  
  144.             HtmlBold (pECB, TEXT("Data Has Control Characters (bHasCtrlChars): "));
  145.             wsprintf (szMsg, TEXT("%u"), bHasCtrlChars);
  146.             HtmlWriteText (pECB, szMsg);
  147.             HtmlLineBreak (pECB);
  148.  
  149.             HtmlBold (pECB, TEXT("Instance of Form Key (nInstance): "));
  150.             wsprintf (szMsg, TEXT("%u"), nInstance);
  151.             HtmlWriteText (pECB, szMsg);
  152.  
  153.             if (dwLength)
  154.                 {
  155.                 HtmlLineBreak (pECB);
  156.                 HtmlBold (pECB, TEXT("Data Sent for Key:"));
  157.                 HtmlLineBreak (pECB);
  158.                 
  159.                 HexDumper (pECB, GetKeyBuffer (hLastKey), dwLength);
  160.                 }
  161.  
  162.             HtmlEndParagraph (pECB);
  163.             }
  164.         
  165.         // Clean up
  166.         FreeKeyList (hKeyList);
  167.         }
  168.  
  169.     HtmlEndPage (pECB);
  170.  
  171.     return HSE_STATUS_SUCCESS;
  172.     }
  173.  
  174. //
  175. // Put the inbound data in a hex dump format
  176. //
  177.  
  178. void HexDumper (EXTENSION_CONTROL_BLOCK *pECB, LPBYTE lpbyBuf, DWORD dwLength)
  179.     {
  180.     DWORD dwSize;
  181.     TCHAR szLine[80];
  182.     TCHAR szHex[3];
  183.     DWORD i;
  184.     DWORD dwPos = 0;
  185.  
  186.     HtmlBeginPreformattedText (pECB);
  187.  
  188.     while (dwLength)
  189.         {
  190.         // Take min of 16 or dwLength
  191.         dwSize = min (16, dwLength);
  192.  
  193.         // Build text line
  194.         wsprintf (szLine, TEXT("  %04X "), dwPos);
  195.  
  196.         for (i = 0 ; i < dwSize ; i++)
  197.             {
  198.             wsprintf (szHex, TEXT("%02X"), lpbyBuf[i]);
  199.             lstrcat (szLine, szHex);
  200.             lstrcat (szLine, TEXT(" "));
  201.             }
  202.  
  203.         // Add spaces for short lines
  204.         while (i < 16)
  205.             {
  206.             lstrcat (szLine, TEXT("   "));
  207.             i++;
  208.             }
  209.  
  210.         // Add ASCII chars
  211.         for (i = 0 ; i < dwSize ; i++)
  212.             {
  213.             if (isprint (lpbyBuf[i]))
  214.                 {
  215.                 wsprintf (szHex, TEXT("%c"), lpbyBuf[i]);
  216.                 lstrcat (szLine, szHex);
  217.                 }
  218.             else
  219.                 {
  220.                 lstrcat (szLine, TEXT("."));
  221.                 }
  222.             }
  223.  
  224.         // Write data to web page
  225.         HtmlWriteTextLine (pECB, szLine);
  226.  
  227.         // Advance positions
  228.         dwLength -= dwSize;
  229.         dwPos += dwSize;
  230.         lpbyBuf += dwSize;
  231.         }
  232.  
  233.     HtmlEndPreformattedText (pECB);
  234.     }